<?php
namespace app\admin\controller;
use think\Controller;
//引入数据库操作文件
use think\Db;
use think\Request;
class News extends Controller
{
    public function index()
    {
        if(!empty($_GET['type'])){
            $type = $_GET['type'];
            $result = Db::name('journalisminfo')
            ->where('journalism_type',$type)
            ->paginate(2);
            $num = count(Db::name('journalisminfo')
                ->where('journalism_type',$type)
                ->select());
        }else{
            $num = count(Db::name('journalisminfo')
                ->select());
            $result = Db::name("journalisminfo")
            ->paginate(2);
        }
        
        $this->assign("num",$num);
        $this->assign("result",$result);
        return $this->fetch("news/newslist");
    }
    //删除新闻
    public function delnews(){
        if(!empty($_GET['newsid'])){
            $newsid = (int)$_GET['newsid'];
            $res = Db::name('journalisminfo')->where('id',$newsid)->delete();
            if($res>0){
                $this->redirect('http://118.24.83.96:80/wojuAdmin/public/admin/News/index');
            }
        }
    }
    //渲染添加页面
    public function addnewsfet(){
        return $this->fetch("news/addnews");
    }
    //渲染修改页面
    public function upnewsfet(){
        if(!empty($_GET['id'])){
            $res = Db::name('journalisminfo')
            ->where('id',$_GET['id'])
            ->find();
        }
        $this->assign("res",$res);
        return $this->fetch("news/upnews");
    }
    //定义操作上传的函数
    function move_uploads($imgTmp,$imgName,$imgType){
        //构造上传图片的名称
        $upload_name = rand(1000000, 10000000);
        if($imgType == "image/jpeg"){
            $filename = "./uploads/".$upload_name.".jpg";
        }else{
            $filename = "./uploads/".$upload_name.".png";
        }
        move_uploaded_file($imgTmp,$filename);
        return $upload_name;
    }
    //添加新闻
    public function addnews(){
          //得到上传文件并检查是否满足条件（图片格式，图片大小）
          $img = $_FILES['images'];
          //得到图片类型
          $imgType = $img['type'];
          //得到图片大小
          $imgSize = $img['size'];
          //得到图片临时存放位置名称
          $imgTmpName = $img['tmp_name'];
          //得到图片的名称
          $imgName = $img['name'];
          //1、检查是否存在存放上传文件的文件夹
          if(file_exists("./uploads")){
             $imgagename = $this->move_uploads($imgTmpName,$imgName,$imgType);
          }else{
              //如果检查到不存在uploads文件则创建目录
              mkdir("./uploads");
             $imgagename = $this->move_uploads($imgTmpName,$imgName,$imgType);
          }
        if(!empty($_POST)){ 
            $title = $_POST['newstitle'];
            $type = $_POST['newstype'];
            $content = $_POST['newscontent'];
            $time = date("Y-m-d  h:i:s");
            $autor = $_POST['autor'];
            $lnum = 0;
           $data = [
               'journalism_type' => $type,
               'journalism_content' => $content,
               'journalism_img' => $imgagename,
               'journalism_title' => $title,
               'journalism_time' => $time,
               'journalism_author' => $autor,
               'journalism_pageview' => $lnum
           ];
           $res = Db::name('journalisminfo')->insert($data);
           if($res>0){
               return "1";
           }
        }
       
    }
   
    public function upnews() {
        //获取修改的新闻id
        $newsid = $_POST['newsid'];
        if(!empty($_FILES['images'])){
            //得到上传文件并检查是否满足条件（图片格式，图片大小）
            $img = $_FILES['images'];
            //得到图片类型
            $imgType = $img['type'];
            //得到图片大小
            $imgSize = $img['size'];
            //得到图片临时存放位置名称
            $imgTmpName = $img['tmp_name'];
            //得到图片的名称
            $imgName = $img['name'];
            //1、检查是否存在存放上传文件的文件夹
            if(file_exists("./uploads")){
                $imgagename = $this->move_uploads($imgTmpName,$imgName,$imgType);
            }else{
                //如果检查到不存在uploads文件则创建目录
                mkdir("./uploads");
                $imgagename = $this->move_uploads($imgTmpName,$imgName,$imgType);
            }
            if(!empty($_POST)){
                $title = $_POST['newstitle'];
                $type = $_POST['newstype'];
                $content = $_POST['newscontent'];
                $autor = $_POST['autor'];
                $data = [
                    'journalism_type' => $type,
                    'journalism_content' => $content,
                    'journalism_img' => $imgagename,
                    'journalism_title' => $title,
                    'journalism_author' => $autor
                ];
                $res = Db::name('journalisminfo')->where('id',$newsid)->update($data);
                if($res>0){
                    return "1";
                }
            }
        }else{
        if(!empty($_POST)){
                $title = $_POST['newstitle'];
                $type = $_POST['newstype'];
                $content = $_POST['newscontent'];
                $autor = $_POST['autor'];
                $data = [
                    'journalism_type' => $type,
                    'journalism_content' => $content,
                    'journalism_title' => $title,
                    'journalism_author' => $autor
                ];
                $res = Db::name('journalisminfo')->where('id',$newsid)->update($data);
                if($res>0){
                    return "1";
                }
              }
        
        }
    }
}